package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateHelper {

	protected static Logger logger = Logger.getLogger(DateHelper.class);

	private static String DATE_FORMAT = "dd/MM/yyyy";
	private static String SHORT_DATE_FORMAT = "dd/MM/yy";
	private static String DATE_H_FORMAT = "dd/MM/yy HH:mm";
	private static SimpleDateFormat sdf;

	public static Date getFecha(String fecha) {
		try {
			if (!"".equals(fecha.trim()))
				return getParser(fecha.trim().length()).parse(fecha.trim());
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	public static String getFechaHora(Date fecha) {
		try {
			return getParser(2).format(fecha);
		} catch (Exception e) {
			logger.error(fecha);
			logger.error(e);
		}
		return null;
	}
	
	public static String getFecha(Date fecha) {
		try {
			return getParser().format(fecha);
		} catch (Exception e) {
			logger.error(fecha);
			logger.error(e);
		}
		return null;
	}

	public static SimpleDateFormat getParser(int fechaSize) {
		if (fechaSize == 2){
			sdf = new SimpleDateFormat(DATE_H_FORMAT);
		} else if (fechaSize == 8)
			sdf = new SimpleDateFormat(SHORT_DATE_FORMAT);
		else
			sdf = new SimpleDateFormat(DATE_FORMAT);
		sdf.setLenient(false);
		return sdf;
	}

	public static SimpleDateFormat getParser() {
		sdf = new SimpleDateFormat(DATE_FORMAT);
		sdf.setLenient(false);
		return sdf;
	}

	public static String getMes(int i) {
		switch (i) {
		case 1:
			return "Enero";
		case 2:
			return "Febrero";
		case 3:
			return "Marzo";
		case 4:
			return "Abril";
		case 5:
			return "Mayo";
		case 6:
			return "Junio";
		case 7:
			return "Julio";
		case 8:
			return "Agosto";
		case 9:
			return "Septiembre";
		case 10:
			return "Octubre";
		case 11:
			return "Noviembre";
		case 12:
			return "Diciembre";
		}
		return "";
	}

	public static int getMes(String month) {
		if (month.equals("Enero"))
			return 1;
		if (month.equals("Febrero"))
			return 2;
		if (month.equals("Marzo"))
			return 3;
		if (month.equals("Abril"))
			return 4;
		if (month.equals("Mayo"))
			return 5;
		if (month.equals("Junio"))
			return 6;
		if (month.equals("Julio"))
			return 7;
		if (month.equals("Agosto"))
			return 8;
		if (month.equals("Septiembre"))
			return 9;
		if (month.equals("Octubre"))
			return 10;
		if (month.equals("Noviembre"))
			return 11;
		if (month.equals("Diciembre"))
			return 12;
		return -1;
	}

	public static String getDay(Date date) {
		Calendar cal = null;
		cal = Calendar.getInstance();
		cal.setTime(date);
		return Integer.toString(cal.get(Calendar.DATE));
	}

	public static String getMonth(Date date) {
		Calendar cal = null;
		cal = Calendar.getInstance();
		cal.setTime(date);
		return Integer.toString(cal.get(Calendar.MONTH));
	}

	public static String getYear(Date date) {
		Calendar cal = null;
		cal = Calendar.getInstance();
		cal.setTime(date);
		return Integer.toString(cal.get(Calendar.YEAR));
	}

	public static int DIAS = 1;
	public static int MESES = 2;
	public static int ANIOS = 3;

	public static int diferenciaFechas(String fec1, String fec2, int valor) {
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		int retorno = 0;
		java.util.Date date1 = null;
		java.util.Date date2 = null;
		try {
			Calendar cal1 = null;
			date1 = df.parse(fec1);
			cal1 = Calendar.getInstance();

			Calendar cal2 = null;
			date2 = df.parse(fec2);
			cal2 = Calendar.getInstance();

			// different date might have different offset
			cal1.setTime(date1);
			long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);

			cal2.setTime(date2);
			long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);

			// Use integer calculation, truncate the decimals
			int hr1 = (int) (ldate1 / 3600000); // 60*60*1000
			int hr2 = (int) (ldate2 / 3600000);

			int days1 = (int) hr1 / 24;
			int days2 = (int) hr2 / 24;

			int dateDiff = days2 - days1;
			int yearDiff = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
			int monthDiff = yearDiff * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);

			if (valor == 1) {
				if (dateDiff < 0)
					dateDiff = dateDiff * (-1);
				retorno = dateDiff;
			} else if (valor == 2) {
				if (monthDiff < 0)
					monthDiff = monthDiff * (-1);
				retorno = monthDiff;
			} else if (valor == 3) {
				if (yearDiff < 0)
					yearDiff = yearDiff * (-1);
				retorno = yearDiff;
			}
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		return retorno + 1;
	}
}
